// pages/recycle/submitform/submitform.js
var app = getApp()
var IP = app.globalData.IP
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text1: '',
    text2: '',
    text3: '',
    text4: '',
    text5: '',

    list: [],

    //时间选择器
    datetimeVisible: false,
    datetime: new Date().getTime(),
    datetimeText: '',

    address: '',
    //  服务信息
    serviceinfo: '',

    openid: wx.getStorageSync('openid'),
    steps: [{

        desc: '用户下单',
        inactiveIcon: 'arrow'

      },
      {

        desc: '免费上门',
        inactiveIcon: 'arrow'

      },
      {

        desc: '检测估价',
        inactiveIcon: 'arrow'
      },
      {

        desc: '极速到账',
        inactiveIcon: 'arrow'
      }
    ],
    servicename:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    //接收eventChannel传来的数据
    const eventChannel = this.getOpenerEventChannel()
    var that = this
    eventChannel.on('dataTosubmitform', function (data) {
      console.log(data)
      that.setData({
        text1: data.text1,
        text2: data.text2,
        text3: data.text3,
        text4: data.text4,
        text5: data.text5,
        servicename:data.servicename

      })
    })
  },

  // 时间选择器
  showPicker() {
    this.setData({
      datetimeVisible: true,
    });
  },
  hidePicker() {

    this.setData({
      datetimeVisible: false,
    });
  },
  onConfirm(e) {
    const {
      value
    } = e?.detail;

    this.setData({
      datetimeText: value,
    });

    this.hidePicker();
  },

  //获取地址列表
  naviTodDeliveryaddress() {
    if (wx.getStorageSync('islogin') == true) {
      wx.navigateTo({
        url: '/pages/usercenter/deliveryAddress/deliveryAddress',
        events: {
          // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
          acceptDataFromDeliveryAddress: data => {
            this.setData({
              address: data
            })

          },
        },
        success: function (res) {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('identity', {
            identity: "serviceDetail"
          })
        }

      })
    } else {
      wx.showModal({
        title: '',
        content: '请登录',
        showCancel: false
      });
      const timer = setTimeout(() => {
        wx.switchTab({
          url: '/pages/usercenter/index/index',
        })
      }, 1200)
      this.setData({
        timer: timer
      });

    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */


  onSubmit() {
    wx.showModal({
      title: '提交订单',
      content: '确认提交订单？',
      complete: (res) => {
        if (res.cancel) {}

        if (res.confirm) {

          this.saveorder()

          
          wx.navigateTo({
            url: '/pages/order/aftersubmit/aftersubmit?recycle='+"yes",
            events: {
              // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
            },
            success: res => {
              // 通过eventChannel向被打开页面传送数据
              res.eventChannel.emit('ordersubmitToAfterSubmit', {
              })

             
            }

          })

        }
      }
    })


  },
   
  saveorder() {
    wx.request({
      url: `http://${IP}:8080/recycle/saverecycleorder`,
      data: {
        recyclename: this.data.servicename + "回收",
        desc: this.data.text2 + " " + this.data.text3 + " "+ this.data.text1 + " " + this.data.text4 + " " + this.data.text5,
        address: this.data.address.provincename + ' ' + this.data.address.cityname + ' ' + this.data.address.countyname + ' ' + this.data.address.detailinfo,
        openid: this.data.openid,

        booktime: this.data.datetimeText,
        telnumber:this.data.address.telnumber



      }

    })

  },

  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})